'use strict';

const Controller = require('egg').Controller;

class LectureRcartController extends Controller {
  async index() {
    const { ctx, service } = this;
    const res = await service.cart.getAllRelation(ctx.state.user.data._id);
    ctx.body = res;
  }
  async show() {
    const { ctx, service } = this;
    const res = await service.lecturer.getRelation(ctx.params.id, ctx.state.user.data._id);
    ctx.body = res;
  }
  async create() {
    const { ctx, service } = this;
    const { _id: uid } = ctx.state.user.data;
    const { cid } = ctx.request.body;
    const res = await service.cart.addInCart(uid, cid);
    console.log(res);
    if (res && res.code === 0) {
      ctx.status = 201;
      ctx.body = {
        code: '20100',
        user_id: uid,
        class_id: cid,
      };
    } else if (res && res.code === 1) { // 购物车中已存在
      ctx.status = 400;
      ctx.body = {
        code: '40000',
        err: '请勿重复添加',
      };
    } else {
      ctx.status = 400;
      ctx.body = {
        code: '40099',
        err: '发生未知错误',
      };
    }
  }
}

module.exports = LectureRcartController;
